package com.ccn.ui.a1ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

/**
 * 1.2.3Canvas高阶使用技巧-变换，状态保存，离屏缓冲，粒子特效
 * <p>
 * canvas状态保存和恢复
 */
public class A1_2_3SaveRestoreView extends View {
    private static final String TAG = "A1_2_3SaveRestoreView";
    private Paint mPaint;

    public A1_2_3SaveRestoreView(Context context) {
        this(context, null);
    }

    public A1_2_3SaveRestoreView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public A1_2_3SaveRestoreView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(4);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        /*canvas.drawRect(200, 200, 700, 700, mPaint);
        canvas.translate(50, 50);
        mPaint.setColor(Color.GRAY);
        canvas.drawRect(0,0,500,500,mPaint);

        canvas.translate(-50, -50);
        canvas.drawLine(0,0,400,500,mPaint);*/

        //使用save() restore()
        /*
        getSaveCount初始值是1
        getSaveCount的值小于1将崩溃
        save getSaveCount+1
        restore getSaveCount-1
         */
        /*canvas.drawRect(200, 200, 700, 700, mPaint);
        Log.e(TAG, "onDraw: "+canvas.getSaveCount());
        int state = canvas.save();
        Log.e(TAG, "onDraw: "+canvas.getSaveCount());
        canvas.translate(50, 50);
        mPaint.setColor(Color.GRAY);
        canvas.drawRect(0,0,500,500,mPaint);

        canvas.save();
        Log.e(TAG, "onDraw: "+canvas.getSaveCount());
        canvas.translate(50, 50);
        mPaint.setColor(Color.BLUE);
        canvas.drawRect(0,0,500,500,mPaint);

//        canvas.restore();
//        Log.e(TAG, "onDraw: "+canvas.getSaveCount());
//        canvas.restore();
        //使用restoreToCount代替restore
        canvas.restoreToCount(state);
//        canvas.restore();如果调用次数过多，getSaveCount的值小于1将崩溃
        Log.e(TAG, "onDraw: "+canvas.getSaveCount());
        canvas.drawLine(0,0,400,500,mPaint);*/

        //离屏绘制
        canvas.drawRect(200,200,700,700,mPaint);

        int layerId = canvas.saveLayer(0, 0, 700, 700, mPaint);
        mPaint.setColor(Color.GRAY);
        Matrix matrix = new Matrix();
        matrix.setTranslate(100,100);
        canvas.setMatrix(matrix);
        canvas.drawRect(0,0,700,700,mPaint);//由于平移操作，导致绘制的矩形超出了图层的大小，所以绘制不完全
        canvas.restoreToCount(layerId);

        mPaint.setColor(Color.RED);
        canvas.drawRect(0,0,100,100,mPaint);
    }
}
